<template>
  <div>
    <el-dialog title="用户注册" :visible.sync="isShow" width="600px" @close="addEvent">

      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="头像">
          <!-- action:上传的接口地址（必传参数）
        http-request:自定义上传
        before-upload:上传前的钩子，常用于上传前限制上传文件大小与类型  return true 正常上传 false 中止上传
        show-file-list：是否显示已上传文件列表  默认是true -->
          <!-- 头像上传   -->
          <!-- <el-upload
            class="avatar-uploader"
            action="/abc/uploads"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="image" :src="image" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload> -->
          <el-upload

            v-model="form.avatar"
            :on-success="onSuccess"
            class="avatar-uploader"
            action="/abc/uploads"
            name="image"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
          >
            <div>
              <img v-if="image" :src="image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </div>
          </el-upload>
        <!-- form表单框 -->
        </el-form-item>
        <el-form-item label="昵称" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="图形码" class="code" prop="code">
          <el-input v-model="form.code" />
          <img class="codeimg" :src="picimg" alt="" @click="addpicimg">
        </el-form-item>
        <el-form-item label="验证码" class="rcode" prop="rcode">
          <el-input v-model="form.rcode" />
          <el-button class="btnrcode" @click="nupic">获取用户验证码</el-button>
        </el-form-item>

      </el-form>
      <template #footer>
        <div style="text-align: center;">
          <el-button @click="addEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sendsms, register, bigsendsms } from '@/api/login'
export default {
  data() {
    return {
      isShow: false,
      image: '',
      picimg: process.env.VUE_APP_BASE_API + '/captcha?type=sendsms&abc=',
      date: '',
      imgurl: '',
      phonelive: '',
      form: {
        avatar: '', //	是	string	头像地址
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        password: '', //	是	string	密码
        rcode: '', //	是	string	验证码
        phone: '',
        code: ''
      },
      rules: {
        username: [{ required: true, message: '请输入名称', trigger: 'change' },
          {
            min: 3, max: 7, message: '请输入3-5位名称', trigger: 'change'
          }],
        email: [{ required: true, message: '请输入邮箱号码', trigger: 'change' },
          {
            min: 17, max: 20, message: '请输入正确的邮箱号码', trigger: 'change'
          }],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'change' },
          {
            min: 11, max: 13, message: '请输入11位的手机号码', trigger: 'change'
          }],
        password: [{ required: true, message: '请输入密码', trigger: 'change' },
          {
            min: 6, max: 16, message: '请输入6到16位密码', trigger: 'change'
          }],
        code: [{ required: true, message: '请输入图片密码', trigger: 'change' },
          {
            min: 4, max: 4, message: '请输入4位图片验证码', trigger: 'change'
          }],
        rcode: [{ required: true, message: '请输入验证码', trigger: 'change' },
          {
            min: 4, max: 4, message: '请输入验证码', trigger: 'change'
          }]

      }

    }
  },
  methods: {
    // 上传前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
      const isLt8M = file.size / 1024 / 1024 < 8

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt8M) {
        this.$message.error('上传头像图片大小不能超过 8MB!')
      }
      return isJPG && isLt8M
    },
    // 上传头像
    async onSuccess(res) {
      // console.log(res)
      this.image = 'niuma/' + res.data.file_path
      this.form.avatar = res.data.file_path
    },
    // 获取注册图片
    async getDateimg() {
      // console.log(Date.now())
      this.date = Date.now()
      await sendsms({ abc: this.data })
      this.picimg = process.env.VUE_APP_BASE_API + '/captcha?type=sendsms&abc=' + this.date
      console.log('获取注册图片成功')
    },
    // 更改注册图片
    addpicimg() {
      this.getDateimg()
      this.picimg = process.env.VUE_APP_BASE_API + '/captcha?type=sendsms&abc=' + this.date
    },
    // 按钮验证码
    async nupic() {
      console.log('qqq')
      const res = await bigsendsms(this.form)
      this.phonelive = JSON.stringify(res.data.captcha)
      this.$message.success(this.phonelive)
      // console.log(res, '牛牛牛了')
    },
    // 表单验证
    async submit() {
      this.$refs.form.validate()
      await register(this.form)
      this.$message.success('注册成功')
      this.addEvent()
    },
    // 清除表单
    addEvent() {
      this.show = false
      this.$refs.form.resetFields()
      this.form = {
        avatar: '',
        username: '',
        email: '', //
        password: '',
        rcode: '',
        phone: '',
        code: ''
      }
      this.image = ''
    }
  }
}
</script>

<style lang="scss" scoped>
 .avatar-uploader{
  text-align: center;

}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;

  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    border: 1px dashed skyblue;
    border-radius: 10px;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    border-radius: 6px;
    width: 178px;
    height: 178px;
    object-fit: cover;
    display: block;
  }
  .code{
    ::v-deep .el-form-item__content{
      display: flex;
      align-items: center;
      .codeimg{
        // vertical-align: top;
        margin-left: 20px;
        width: 55%;
        height: 40px;
      }
        }
  }
  .rcode{
    ::v-deep .el-form-item__content{
      display: flex;
      align-items: center;
      .btnrcode{
        width: 50%;
        height: 40px;
        margin-left: 20px;
      }
    }
  }
</style>
